#coding:utf-8 
from __future__ import absolute_import

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
import os


from twisted.python import log

log.startLogging(sys.stdout)

__main__ = None

class ClientApp(QtGui.QApplication):
    def __init__(self,argv=[]):
        super(ClientApp, self).__init__(argv)
        
                        

app = ClientApp(sys.argv)
app.setQuitOnLastWindowClosed(False)

from core import PyQt4Reactor
PyQt4Reactor.install()
from twisted.internet import reactor

from core.Main import MainClient
from include import Utils
from include.Addins import join

class StartUp(QtCore.QObject): ######TODO
        def __init__(self,parent):
                super(StartUp,self).__init__(parent)
                self.config = None
                self.homeDir = Utils.getHomeDir()
                self.rootDir = unicode(self.homeDir)
                Utils.loadConfig(self)
                profiles = Utils.getProfiles(self.rootDir)
                lastProfile = join(self.config["jid"],"-profile")
                if  not lastProfile in profiles:
                        if profiles.__len__() != 0:
                                self.homeDir = os.path.join(self.rootDir,profiles[0])
                                Utils.loadConfig(self)
                        else:
                                os.remove(os.path.join(self.rootDir,"config.ini"))
                else:
                        self.homeDir = os.path.join(self.rootDir,lastProfile)
                        Utils.loadConfig(self)
def run():
    global  __main__
    home = Utils.getHomeDir()
    root   = unicode(home)
    
    __main__ = MainClient()
    __main__.show()
    reactor.run()

if __name__ == "__main__":
    run()